#@adminLayout()
#define main()
<div class="jbolt_page" data-key="#(pmkey)">
    <div class="jbolt_page_title">
        <div class="row">
            <div class="col-9">
                <h1>商品分类管理</h1>
            </div>
            <div class="col-3">
            </div>
        </div>
    </div>
    <div class="jbolt_page_content">
        <div class="row">
            <div class="col-sm-3">
                <div class="card">
                    <div class="card-header  ">商品分类树</div>
                    <div class="card-body">
                        <div id="retailerCommodityTypeTree"></div>
                    </div>
                </div>
            </div>
            <div class="col-sm-9">
                #ajaxPortal("/auth/mall/type/commodity/edit_Commodity_Type","retailerCommodityTypePortal",true)
            </div>
        </div>

    </div>
</div>
#end
#define css()
<link href="/assets/plugins/jstree/themes/default/style.min.css" rel="stylesheet">
#end

#define js()
<script src="/assets/plugins/jstree/jstree.min.js"></script>
<script type="text/javascript">
    var retailerCommodityTypeTree=$("#retailerCommodityTypeTree");
    var retailerCommodityTypePortal=$("#retailerCommodityTypePortal");


    function readRetailerCommodityTypeTree(){
        Ajax.get("/auth/mall/type/commodity/mgrTree",function(data){
            var treeDatas=data.data;
            retailerCommodityTypeTree.jstree({
                'core' : {
                    'data' :treeDatas,
                    'check_callback' : true
                },
                'plugins' : ["themes",'dnd','contextmenu',"wholerow"],
                "contextmenu":{
                    "items":
                        {
                            "create":{
                                "label":"创建子类目",
                                "action":function(data){
                                    var inst = $.jstree.reference(data.reference),
                                        obj = inst.get_node(data.reference);//获得当前节点,可以拿到当前节点所有属性
                                    //var dd=(new Date().getTime()).toString();
                                    //var name="新分类_"+dd.substring(10)+parseInt(Math.random()*10);
                                    var pid=obj.id;
                                    var url="/auth/mall/type/commodity/add_Commodity_Type/"+pid;
                                    retailerCommodityTypePortal.ajaxPortal(true,url,true);
                                    /* createNewGoodsBackCategory({"category.pid":pid,"category.name":name},function(categoryId){
                                        inst.create_node(obj, {"id":categoryId,"parent":pid,"text":name}, 12, function (new_node) {
                                            setTimeout(function () {
                                                inst.deselect_node(obj);
                                                inst.select_node(new_node);
                                            },100);

                                        });
                                    }); */

                                }
                            },
                            "delete":{
                                "label":"删除此分类",
                                "action":function(data){
                                    var inst = $.jstree.reference(data.reference),
                                        obj = inst.get_node(data.reference);
                                    delRetailerCommodityType(obj.id,function(){
                                        if(inst.is_selected(obj)) {
                                            inst.delete_node(inst.get_selected());
                                        }
                                        else {
                                            inst.delete_node(obj);
                                        }
                                    });

                                }
                            }

                        }



                }
            })

                .on('changed.jstree', function (e, data) {
                    if(data&&data.node){
                        changeRetailerCommodityTypeFormPortal(data.node.id);
                    }
                });
        });

    }
    var delNodeId=0;
    //切换表单
    function changeRetailerCommodityTypeFormPortal(id){
        if(id==delNodeId){
            id=0;
        }
        var url="/auth/mall/type/commodity/edit_Commodity_Type/"+(id?id:0);
        retailerCommodityTypePortal.ajaxPortal(true,url,true);
    }


    //删除
    function delRetailerCommodityType(id,callback){
        if(id==0){
            LayerMsgBox.alert("不能删除根节点",2);
            return;
        }
        LayerMsgBox.loading("执行中...");
        Ajax.get("/auth/mall/type/commodity/delete_Commodity_Type/"+id,function(data){
            LayerMsgBox.success("删除成功",1000);
            delNodeId=id;
            if(callback){
                callback();
            }
        });

    }
    //提交表单
    function submitRetailerCommodityTypeForm(){
        var form=$("#retailerCommodityTypeForm");
        if(FormChecker.check(form)){
            LayerMsgBox.loading("执行中...");
            var formData=form.serialize();
            var action=form.attr("action");
            Ajax.post(action,formData,function(data){
                LayerMsgBox.success("操作成功",500,function(){
                    refreshTreeAll(data.data);
                });
            });
        }

    }
    //刷新整棵树
    function refreshTreeAll(selectId){
        Ajax.get("/auth/mall/type/commodity/mgrTree/"+selectId,function(data){
            var treeDatas=data.data;
            retailerCommodityTypeTree.jstree(true).deselect_all();
            retailerCommodityTypeTree.jstree(true).settings.core.data=treeDatas;
            retailerCommodityTypeTree.jstree(true).refresh();

            setTimeout(function(){
                retailerCommodityTypeTree.jstree(true).select_node({id:selectId,selected:true});
            },100);
        });
    }
    //刷新节点
    function refreshTreeData(node){
        retailerCommodityTypeTree.jstree(true).set_text({id:node.id},node.name);
    }

    $(function(){
        readRetailerCommodityTypeTree();
    });
</script>
#end

